import React, { useState, useEffect } from 'react';
import { Form, Select, Radio, Divider, Input, DatePicker } from 'antd';
import Difference from '@/pages/WorkFlow/components/FormControls/components/Difference';
import styles from '@/pages/WorkFlow/components/CustomComponents/Recruitment/index.less';
// import { genID } from '@/pages/WorkFlow/utils';
import moment from 'moment';

// 业务表单显示，为了不影响流程的form表单
const BusinessForm = ({ form, approveData, isPostRankType, formLayout, wrapperLayout, tag }) => {
    const [startTimeValue, setStartTimeValue] = useState(null);
    const [compareDataMap, setCompareDataMap] = useState({}); // 差异化对比映射数据源（非表格）
    // const [dimensionDiffData, setDimensionDiffData] = useState({}); //  维度新增和删除数据，注意：编辑是json字符串要parse

    const { getFieldDecorator, setFieldsValue } = form;

    // 给职级表单赋值
    useEffect(() => {
        if (approveData) {
            const {
                institutionType,
                institutionNo,
                institutionIntroduce,
                startTime,
                auditEffectFlag,
                modifyRemark,
                rankValue,
                rankDesc,
                // rankDimensions = [], // 维度表格数据
                rankName,
                rankTypeName,
                rankTypeDesc,
                oldBusinessData, // 差异化数据源
                compareDataField // 差异化字段
            } = approveData?.operationType === 2 ? approveData?.oldBusinessData : approveData;

            const { updateFieldNames } = compareDataField || {};
            // const { add: tempAddData = [], delete: tempDeleteData = [], edit: tempEdit = [] } = seniorUpdateContent; // 变更的数据

            let formObj = {
                institutionType,
                institutionNo,
                institutionIntroduce,
                rankValue,
                rankDesc,
                auditEffectFlag,
                rankName,
                modifyRemark
                // rankDimensions: [...rankDimensions, ...tempDeleteData] // 新增的不往列表里面添加
            };

            if (isPostRankType) {
                formObj = {
                    applyDate: moment().format('YYYY-MM-DD'),
                    institutionType,
                    institutionNo,
                    institutionIntroduce,
                    rankTypeName,
                    rankTypeDesc,
                    auditEffectFlag,
                    modifyRemark
                };
            }

            if (auditEffectFlag === 1) {
                setStartTimeValue(moment(startTime));
            }
            setFieldsValue(formObj);

            // setDimensionDiffData({
            //     addArr: tempAddData,
            //     deleteArr: tempDeleteData,
            //     editArr: tempEdit
            // });

            // 将有变更的数据进行映射
            if (updateFieldNames && updateFieldNames?.length > 0) {
                const obj = {};
                updateFieldNames.forEach(item => {
                    obj[item] = oldBusinessData[item];
                });
                setCompareDataMap(obj);
            }
        }
    }, [approveData, setFieldsValue]);

    // 表格单元格是否修改

    // 暂时隐藏，产品需求
    // const colChangeText = (record, rowuuid, field) => {
    //     if (record && rowuuid && field) {
    //         let obj = {};
    //         const { editArr = [] } = dimensionDiffData;
    //         editArr.forEach(item => {
    //             const tempObj = JSON.parse(item);
    //             if (Object.keys(tempObj).includes(record[rowuuid])) {
    //                 obj = tempObj;
    //             }
    //         });
    //         const changeArr = Object.values(obj);
    //         const changeObj = changeArr.find(item => item[field]);
    //         if (changeObj) {
    //             return changeObj[field];
    //         }
    //         return undefined;
    //     }
    //     return undefined;
    // };
    // 暂时隐藏，产品需求
    // const columns = [
    //     {
    //         title: '维度',
    //         dataIndex: 'dimensionTypeName',
    //         width: '50%',
    //         render: (text, record) => (
    //             <Difference table tip={colChangeText(record, 'dimensionTypeCode', 'dimensionTypeName')}>
    //                 {text}
    //             </Difference>
    //         )
    //     },
    //     {
    //         title: '要求',
    //         dataIndex: 'demand',
    //         render: (text, record) => {
    //             const { dimensionTypeCode } = record;
    //             const { addArr = [], deleteArr = [] } = dimensionDiffData;
    //             return (
    //                 <Difference table tip={colChangeText(record, 'dimensionTypeCode', 'demand')}>
    //                     {text}
    //                     {deleteArr.find(item => item.dimensionTypeCode === dimensionTypeCode) && (
    //                         <Tag color="red" style={{ marginLeft: 5 }}>
    //                             删除
    //                         </Tag>
    //                     )}
    //                     {addArr.find(item => item.dimensionTypeCode === dimensionTypeCode) && (
    //                         <Tag style={{ marginLeft: 5 }} color="green">
    //                             新增
    //                         </Tag>
    //                     )}
    //                 </Difference>
    //             );
    //         }
    //     }
    // ];

    // const auditEffectDiff = () => {
    //     const { auditEffectFlag = '', startTime } = compareDataMap;
    //     if (String(auditEffectFlag).length > 0) {
    //         if (auditEffectFlag === 1) {
    //             return `指定日期生效：${startTime && moment(startTime).format('YYYY-MM-DD')}`;
    //         }
    //         if (auditEffectFlag === 0) {
    //             return '审核通过即可生效';
    //         }
    //     }
    //     return undefined;
    // };

    return (
        <Form layout="horizontal" className={styles.postEdit} {...wrapperLayout}>
            {!isPostRankType && (
                <Form.Item label="职级序列">
                    <Difference tip={tag === 'OrgRankApply' ? undefined : compareDataMap?.rankTypeName}>
                        <div>{approveData?.rankTypeName || approveData?.oldBusinessData?.rankTypeName}</div>
                    </Difference>
                </Form.Item>
            )}

            <Form.Item label="申请日期">
                <div>{approveData?.applyDate || approveData?.oldBusinessData?.applyDate}</div>
            </Form.Item>

            <Form.Item label="制度名称">
                <Difference tip={compareDataMap?.institutionName}>
                    <div>{approveData?.institutionName || approveData?.oldBusinessData?.institutionName}</div>
                </Difference>
            </Form.Item>

            <Form.Item label="制度类型">
                {getFieldDecorator('institutionType', {
                    rules: [{ required: true }]
                })(
                    <Select disabled>
                        <Select.Option value="personnel">人事制度</Select.Option>
                    </Select>
                )}
            </Form.Item>

            <Form.Item label="制度编号">
                {getFieldDecorator('institutionNo', {
                    rules: [{ required: true }]
                })(<Input disabled placeholder="请输入制度编号" />)}
            </Form.Item>

            <Form.Item label="制度简介">
                {getFieldDecorator('institutionIntroduce', {
                    rules: [{ required: true }]
                })(
                    <Input.TextArea disabled placeholder="请输入制度简介" />
                )}
            </Form.Item>

            <Form.Item label="生效日期" style={{ marginBottom: 0 }}>
                {/* <Difference tip={auditEffectDiff()}> */}
                <div>
                    {getFieldDecorator('auditEffectFlag', {
                        rules: [{ required: true }]
                    })(
                        <Radio.Group disabled>
                            <Radio value={0}>审核通过即可生效</Radio>
                            <Radio value={1}>指定日期生效</Radio>
                        </Radio.Group>
                    )}
                    {form.getFieldValue('auditEffectFlag') === 1 &&
                        getFieldDecorator('startTime', { initialValue: startTimeValue })(
                            <DatePicker disabled format="YYYY-MM-DD" />
                        )}
                </div>
                {/* </Difference> */}
            </Form.Item>

            <Divider />
            <div className={styles.title}>制度内容</div>
            <Form {...formLayout}>
                {isPostRankType && (
                    <>
                        <Form.Item label="职级序列">
                            <Difference tip={compareDataMap?.rankTypeName}>
                                {getFieldDecorator('rankTypeName', {
                                    rules: [{ required: true }]
                                })(<Input disabled placeholder="请输入职级序列" />)}
                            </Difference>
                        </Form.Item>
                        <Form.Item label="描述">
                            <Difference tip={compareDataMap?.rankTypeDesc}>
                                {getFieldDecorator('rankTypeDesc', {
                                    rules: [{ required: true }]
                                })(
                                    <Input.TextArea disabled maxLength={1000} placeholder="请输入职级序列描述，不超过 1000 字" />
                                )}
                            </Difference>
                        </Form.Item>
                    </>
                )}

                {!isPostRankType && (
                    <>
                        <Form.Item label="职级名称">
                            <Difference tip={compareDataMap?.rankName}>
                                {getFieldDecorator('rankName', {
                                    rules: [{ required: true }]
                                })(<Input disabled placeholder="请输入职级名称" />)}
                            </Difference>
                        </Form.Item>
                        <Form.Item label="职级">
                            <Difference tip={compareDataMap?.rankValue}>
                                {getFieldDecorator('rankValue', {
                                    rules: [{ required: true }]
                                })(<Input disabled placeholder="请输入字母与数字" />)}
                            </Difference>
                        </Form.Item>
                        <Form.Item label="描述">
                            <Difference tip={compareDataMap?.rankDesc}>
                                {getFieldDecorator('rankDesc')(
                                    <Input.TextArea disabled maxLength={1000} placeholder="请输入职级描述，不超过 1000 字" />
                                )}
                            </Difference>
                        </Form.Item>
                        {/* <Form.Item label="评定维度">
                            {getFieldDecorator('rankDimensions', {
                                valuePropName: 'dataSource',
                                rules: [{ required: true }]
                            })(<Table columns={columns} pagination={false} rowKey={() => genID(6)} />)}
                        </Form.Item> */}
                    </>
                )}

                <Form.Item label="原因">
                    {getFieldDecorator('modifyRemark', {
                        rules: [{ required: true }]
                    })(
                        <Input.TextArea disabled placeholder="请输入原因，不超过 200 字" />
                    )}
                </Form.Item>
            </Form>
        </Form>
    );
};

export default Form.create()(BusinessForm);
